﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Saker.Kernel.Model;
using System.Data;
using Saker.Kernel.BLL;

namespace Saker.Web.Boss.Sys.Role
{
    public partial class RolesList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }
        public void BindData()
        {
            RepRoleList.DataSource = new SysRoleBLL().GetAll();
            RepRoleList.DataBind();
        }

        protected void RepRoleList_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "EditRole")
            {
                Response.Redirect("RoleAdd.aspx?cmd=Edit&roleID=" + e.CommandArgument.ToString());
            }
            if (e.CommandName == "DeleteRole")
            {
                if (new SysRoleBLL().DeleteByRoleID(int.Parse(e.CommandArgument.ToString())) > 0)
                {
                    BindData();
                    ScriptManager.RegisterStartupScript((Page)this, this.Page.GetType(), "alert", "$(function () { top.jBox.tip('角色删除成功！', 'success'); });", true);
                }
            }
            if (e.CommandName == "SetAuthority")
            {
                Response.Redirect("RoleAuthority.aspx?roleID=" + e.CommandArgument.ToString());
            }  
        }

        protected void RepRoleList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            //CheckBoxList cbList = (CheckBoxList)e.Item.FindControl("cbAuthority");
            //Label lab_ID = (Label)e.Item.FindControl("lab_ID");
            //IEnumerable<SysAuthorityDir> list = new SysAuthorityDirBLL().GetAll();
            //cbList.DataSource = list;
            //cbList.DataTextField = "AuthorityName";
            //cbList.DataValueField = "AuthorityTag";
            //cbList.RepeatColumns = list.Count<SysAuthorityDir>();
            //cbList.DataBind();
            //DataTable dt = new SysModuleAuthorityListBLL().GetAllToTable();
            //DataRow[] rows = dt.Select("ModuleID='" + lab_ID.Text + "'");
            //foreach (DataRow row in rows)
            //{
            //    cbList.Items.FindByValue(row["AuthorityTag"].ToString()).Selected = true;
            //}
        }
    }
}